package com.kh.pajx.sop.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.IHsEquSwitchDao;
import com.kh.pajx.sop.dao.IHsEquipmentInfoDao;
import com.kh.pajx.sop.domain.HsEquSwitch;
import com.kh.pajx.sop.domain.HsEquipmentInfo;
import com.kh.pajx.sop.domain.PageBean;
import com.kh.pajx.sop.domain.SysInsideUser;
import com.kh.pajx.sop.service.IHsEquSwitchService;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.kh.pajx.sop.util.sop.PropertyValidate;

@Service
@Transactional
public class HsEquSwitchServiceImpl extends BaseServiceImpl<HsEquSwitch> implements IHsEquSwitchService {

	@Resource
	private IHsEquSwitchDao hsEquSwitchDao;
	@Resource
	private IHsEquipmentInfoDao hsEquipmentInfoDao;

	public HsEquSwitchServiceImpl() {
	}

	@Override
	public void save(HsEquSwitch demo) {
		hsEquSwitchDao.save(demo);
	}



	@Override
	public PageBean getPageBean(int pageNum, int pageSize,
			QueryHelper queryHelper) {
		return hsEquSwitchDao.getPageBean(pageNum, pageSize, queryHelper);
	}

	@Override
	public void save(String equNos,String equOpenTime,String equEndTime,String userId,String remark) throws Exception {
		if(StringUtils.isNotEmpty(equNos)){
			String[] equimpNos = equNos.split(",");
			for (int i = 0;i< equimpNos.length; i++) {
				HsEquSwitch equSwitch = new HsEquSwitch();
				equSwitch.setRelationStatusFlag("1");
				 if(StringUtils.isNotBlank(equimpNos[i].trim())&&!PropertyValidate.validateNumber(equimpNos[i].trim(),10)){
					    throw new Exception("设备编号不合法");
					}
				 if(StringUtils.isNotBlank(equOpenTime.replaceAll(":",""))&&!PropertyValidate.validateNumberCode(equOpenTime.replaceAll(":",""),4)){
				    	throw new Exception("设备考勤开始时间不合法");
				    }
				 if(StringUtils.isNotBlank(equEndTime)&&!PropertyValidate.validateNumberCode(equEndTime.replaceAll(":",""),4)){
				    	throw new Exception("设备考勤结束时间不合法");
				    }
				 if(StringUtils.isNotBlank(remark)&&!PropertyValidate.validateStrByLength(remark,200)){
					    throw new Exception("设备考勤时间段-备注信息不合法");
					}
				equSwitch.setEquNo(Long.valueOf(equimpNos[i].trim()));
				equSwitch.setEquOpenTime(equOpenTime.replaceAll(":",""));
				equSwitch.setRemark(remark);
				if(StringUtils.isNotEmpty(equEndTime)&&!equEndTime.equals("")){
					equEndTime = equEndTime.replaceAll(":","");
				}
				equSwitch.setEquEndTime(equEndTime);
				Date date = new Date();
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
				String dateStr = sdf.format(date);
				equSwitch.setCreateTime(dateStr);
				equSwitch.setCreateOperId(userId);
				hsEquSwitchDao.save(equSwitch);
			}
		}
	}


	@Override
	public int deleteByequNo(String swhIds) {
		int returnNum=0;
		if(StringUtils.isNotEmpty(swhIds)){
			String[] switchIds = swhIds.split(",");
			returnNum = switchIds.length;
			hsEquSwitchDao.updateByNos(switchIds);
		}
		return returnNum;
	}

	/*@Override
	public List<HsEquipmentInfo> getByIds(String[] equNos) {
		Long[] nos = new Long[equNos.length];
			 for(int i = 0;i<equNos.length;i++){
				 nos[i]=Long.valueOf(equNos[i]);
			 }
			 return hsEquipmentInfoDao.getByIds(nos);
	}*/
	
	@Override
	public List<HsEquSwitch> getByIds(String[] uuids) {
		return hsEquSwitchDao.getByIds(uuids);
	}

	@Override
	public List<HsEquipmentInfo> findByCon() {
		return hsEquipmentInfoDao.findByCondition();
	}

	@Override
	public List<SysInsideUser> findUserById(String[] isuIds) {
		
		return hsEquipmentInfoDao.findInUser(isuIds);
	}
}
